package com.ttc.mBackupK16.BackupContacts;

import java.util.ArrayList;

import com.ttc.mBackupK16.R;
import com.ttc.mBackupK16.BackupStorage.DropBox;
import com.ttc.mBackupK16.BackupStorage.Email;
import com.ttc.mBackupK16.BackupStorage.FileItemStorage;
import com.ttc.mBackupK16.BackupStorage.SDCard;
import com.ttc.mBackupK16.BackupStorage.StorageObject;
import com.ttc.mBackupK16.BackupStorage.Webservice;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ManageFilesBackupActivity extends Activity implements OnClickListener{

	Button btnDeleteFileBackup, btnCancelDelelteFile;
	CheckBox chBCheckAllContacts;
	ListView lstFilesBackup;
	CustomListAdapter adapter;
	TextView txtContactsNumber, txtContactsNumberChecked;
	StorageObject storageObject;
	ArrayList<FileItemStorage> listFileItem = new ArrayList<FileItemStorage>();
	ArrayList<Contact> arrFileBackup = new ArrayList<Contact>();
	ProgressDialog progressDialog;
	String resultDelete = "";
	
	final Handler handler = new Handler();
	final Runnable mUpdateResultGetBackupFiles = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			adapter = new CustomListAdapter(ManageFilesBackupActivity.this, R.id.lstContacts, arrFileBackup, txtContactsNumberChecked);
			adapter.isListFile = true;
			lstFilesBackup.setAdapter(adapter);
			txtContactsNumber.setText("/" + lstFilesBackup.getCount());
			
			progressDialog.dismiss();
		}
	};
	
	final Runnable mUpdateResultDeleteBackupFiles = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub			
			adapter.iNumItemChecked = 0;
			txtContactsNumberChecked.setText("0");
			adapter.notifyDataSetChanged();
			txtContactsNumber.setText("/" + lstFilesBackup.getCount());
			
			showDeleteFileResultDialog(resultDelete);
			progressDialog.dismiss();
		}
	};
	
	public final static int SDCARD = 0;
	public final static int GMAIL = 1;
	public final static int DROPBOX = 2;
	public final static int MYSERVICE = 3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contacts_list);
		
		Bundle b = this.getIntent().getExtras();
		switch (b.getInt("storage")) {
		case SDCARD:
			storageObject = new SDCard();
			break;
		case GMAIL:
			storageObject = new Email();
			break;
		case DROPBOX:
			storageObject = new DropBox();
			break;
		case MYSERVICE:
			storageObject = new Webservice();
			break;

		default:
			break;
		}
		
		btnDeleteFileBackup = (Button)findViewById(R.id.btnBackupContacts);
		btnDeleteFileBackup.setText("Delete");
		btnDeleteFileBackup.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_btn_delete, 0, 0, 0);
		
		btnCancelDelelteFile = (Button)findViewById(R.id.btnCancelContacts);
		btnCancelDelelteFile.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_btn_back_home, 0, 0, 0);
		
		chBCheckAllContacts = (CheckBox)findViewById(R.id.chBCheckAllContacts);
		lstFilesBackup = (ListView)findViewById(R.id.lstContacts);
		txtContactsNumber = (TextView) findViewById(R.id.txtContactsNumber);
		txtContactsNumberChecked = (TextView) findViewById(R.id.txtContactsNumberChecked);
		
		btnDeleteFileBackup.setOnClickListener(this);
		btnCancelDelelteFile.setOnClickListener(this);
		chBCheckAllContacts.setOnClickListener(this);
			
		Thread threadGetBackupFiles = new Thread(){
			public void run() {			
				listFileItem = storageObject.getFilesList("/mBackup/Contacts");
				arrFileBackup = new ArrayList<Contact>();
				for(int i = 0; i< listFileItem.size(); i++){
					Contact fileBackupInfo = new Contact(listFileItem.get(i).getFileName(), "size: " + listFileItem.get(i).getFileLength() + "KB");
					arrFileBackup.add(fileBackupInfo);
				}
				
				handler.post(mUpdateResultGetBackupFiles);
			};
		};
		threadGetBackupFiles.start();
		
		// Create and show ProgressDialog
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("Getting backup files....");
		progressDialog.show();
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnBackupContacts:
			if(Integer.parseInt(txtContactsNumberChecked.getText().toString()) == 0){
				Toast.makeText(getBaseContext(), "No found any files checked!", Toast.LENGTH_SHORT).show();
			}else{
				showDeleteFileDialog();
			}
			break;
		case R.id.btnCancelContacts:
			this.finish();
			break;
		case R.id.chBCheckAllContacts:
			for(int i = 0; i< arrFileBackup.size(); ++i){
				arrFileBackup.get(i).setChecked(chBCheckAllContacts.isChecked());
			}
			adapter.notifyDataSetChanged();
			if(chBCheckAllContacts.isChecked()){
				txtContactsNumberChecked.setText(lstFilesBackup.getCount() + "");
				adapter.iNumItemChecked = lstFilesBackup.getCount();
			}else{
				txtContactsNumberChecked.setText("0");
				adapter.iNumItemChecked = 0;
			}
			break;
		default:
			break;
		}
	}
	
	private void showDeleteFileDialog(){
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		alertDialog.setTitle("Are you sure ?");
		alertDialog.setMessage("Files which you checked, will be delete!");
		alertDialog.setIcon(R.drawable.ic_warning);
		alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				Thread threadGetBackupFiles = new Thread(){
					public void run() {			
						for(int i = 0; i< arrFileBackup.size(); i++){
							if(arrFileBackup.get(i).isChecked()){
								if(storageObject.deleteFile(listFileItem.get(i).getPath())){
									resultDelete += "- " + listFileItem.get(i).getFileName() + ": success!" + "\n";
									arrFileBackup.remove(i);
									listFileItem.remove(i);
									i--;
								}
								else{
									resultDelete += "- " + listFileItem.get(i).getFileName() + ": fail!" + "\n";
								}
							}
						}
						
						handler.post(mUpdateResultDeleteBackupFiles);
					};
				};
				threadGetBackupFiles.start();
				
				// Create and show ProgressDialog
				progressDialog = new ProgressDialog(ManageFilesBackupActivity.this);
				progressDialog.setMessage("Deletting backup files....");
				progressDialog.show();
			}
		});
		alertDialog.setNegativeButton("Cancel", null);
		alertDialog.show();
	}
	
	private void showDeleteFileResultDialog(String result){
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
		alertDialog.setTitle("Result delete file backup");
		alertDialog.setIcon(R.drawable.ic_result);
		alertDialog.setMessage(result);
		alertDialog.setPositiveButton("Ok", null);
		alertDialog.show();
	}
}
